package com.gosuncn.gbstack;

import android.util.Log;

import com.sip.stream.gbstack.GS28181SDK;

import java.nio.ByteBuffer;

public class JniGS28181Agent {
    private static final String TAG = JniGS28181Agent.class.getSimpleName();

    static {
        System.loadLibrary("androidgs28181");
    }

    private static JniGS28181Agent agent;

    private JniGS28181Agent() {}

    public static JniGS28181Agent getInstance() {
        if (agent == null) {
            synchronized (JniGS28181Agent.class) {
                agent = new JniGS28181Agent();
            }
        }
        return agent;
    }

    /**
     * 发送-告警通知 - OK
     */
    public native int alarmNotify(String deviceId, String priority, String method, String time, String content, int alarmType, int eventType, double longitude, double latitude, String extension);

    /**
     * 响应-告警通知-订阅 - OK
     */
    public native int alarmNotifyToSub(int alarmSubId, String deviceId, String priority, String method, String time, String content, int alarmType, int eventType, double longitude, double latitude, String extension);

    /**
     * 收到目录订阅 - OK
     */
    public native int catalogNotifyToSub(int subId);


    /**
     * 解码 - G711音频 - OK
     */
    public native byte[] decodeG711(byte[] data);

    /**
     * 编码 - G711音频 - OK
     */
    public native byte[] encodeG711(byte[] data);

    /**
     * 强制停止语音通话 - OK
     */
    public native int forceStopInviteVoiceTalk();

    /**
     * 强制停止RTP - OK
     */
    public native int forceStopRTP(boolean isForce);


    /**
     * 生成SN号 - OK
     */
    public native int generateXmlSn();

    /***
     * 获取-SDK信息 - OK
     */
    public native String getSDKInfo();


    /**
     * 连接服务器 - Ok
     * @param deviceId  deviceId
     * @param localIp       本地IP
     * @param localPort     本地端口
     * @param cmdTranType   连接方式 UDP | TCP
     * @param gB28181Version GB28181 版本
     * @return
     */
    public native int initSDK(String deviceId, String localIp, int localPort, int cmdTranType, int gB28181Version);


    /**
     * 输入-音频流 - OK
     */
    public native int inputAudioStream(byte[] data, long presentationTimeUs);


    /**
     * 输入-视频流 - OK
     */
    public native int inputVideoStream(ByteBuffer byteBuffer, int i, long presentationTimeUs);

    /**
     * 邀请-语音广播-播放 - OK
     */
    public native int inviteAudioPlayRequest(String key, int streamTranType);


    /**
     * 邀请-视频播放 - OK
     */
    public native int inviteVideoPlayRequest(String srcDeviceID, int streamTranType);

    /**
     * 心跳-重新注册 - OK
     */
    public native int keepaliveRregister();

    /***
     * 是否打开日志 - OK
     * @param isOpen
     */
    public native int logSwitch(boolean isOpen);


    /**
     * 响应-位置通知-订阅 - OK
     */
    public native int mobilePosNotifyToSub(int posSubId, String deviceId, String time, double longitude, double latitude, double speed, double direction, int altitude);


    /**
     * 注册 - OK
     */
    public native int register(String deviceId, String serverIp, int serverPort, int registerTime, String account, String password);


    /**
     * 释放会话 - OK
     */
    public native int releaseSession(long sessionHandle);


    /**
     * 释放扩展数据会话 - OK
     */
    public native int releaseTransDataSession(int sessionHandle);


    /**
     * 响应-设备目录-查询 - OK
     */
    public native int respondQueryDeviceCatalog2(String json, double longitude, double latitude);


    /**
     * 响应-设备信息-查询 - OK
     */
    public native int respondQueryDeviceInfo(long sessionHandle, String deviceId, String deviceName, String manufacturer, String model, String firmware, int channel);


    /**
     * 响应-设备状态-查询 - OK
     */
    public native int respondQueryDeviceStatus(long sessionHandle, String deviceId, String date, String errReason, boolean isRemoteAudioOrVideo, boolean isRecord, boolean isOnline, boolean isStatusOk);


    /**
     * 发送-扩展数据 -- OK
     */
    public native int respondTransData(boolean status, String json, int type);

    /**
     * 发送-心跳包 - OK
     */
    public native int sendHeartBeat();


    /**
     * 设置-音频参数 - OK
     * @param audioCodeId  1： G711A  5: G711U
     * @param sample 采样率
     * @param bitRate 比特率
     */
    public native int setAudioParams(int audioCodeId, int sample, int bitRate);


    /**
     * 设置视频参数 -- OK
     * @param videoCodeId  5: H265 2: H264
     * @param ppi          height > 1000 ? 6 :5
     * @param rateType     1:固定码率 2: 动态码率
     * @param videoBitrate 视频码率
     * @param videoFps     帧率
     */
    public native int setVideoParams(int videoCodeId, int ppi, int videoFps, int rateType, int videoBitrate);

    /**
     * 测试删除PS流 - OK
     */
    public native byte[] testRemovePS(byte[] bArr);

    /**
     * 网关-扩展消息 - OK
     */
    public native int transDataGateway(String xmlData);


    /**
     * 扩展消息 - OK
     */
    public native int transdataMessage(String str);

    /***
     * 发送扩展SIP指令 - OK
     */
    public native int transDatas(String xmlData);

    /**
     * 卸载SDK - OK
     */
    public native int uninitSDK();

    /***
     * 取消注册 - OK
     */
    public native int unregister();


    /***
     * 流事件通知
     * @param type
     * @param serverGBCode
     * @param json
     */
    public void streamNotifyCallback(int type, String serverGBCode, String json) {
        Log.i(TAG, "应用层收到流事件通知：type= " + type + ",serverGBCode=" + serverGBCode + ",json=" + json);
        GS28181SDK.getInstance().handleStreamNotifyCallback(type, serverGBCode, json);
    }


    /***
     * 同步设备时间
     * @param time
     */
    public void syncDevTimeCallback(String time) {
        Log.i(TAG, "syncDevTimeCallback: " + time);
        GS28181SDK.getInstance().syncDevTime(time);
    }

    /***
     * 收到查询事件通知   -------------- OK GS28181SDK -------------
     */
    public void queryNotifyCallback(int type, long sessionHandle, String serverGBCode, String json) {
        Log.i(TAG, "收到查询事件通知：type= " + type + ",serverGBCode=" + serverGBCode + ",json=" + json + ",sessionHandle=" + sessionHandle);
        GS28181SDK.getInstance().handleQueryNotifyCallback(type, sessionHandle, serverGBCode, json);
    }


    /**
     * 查询 JSON 回调
     */
    public String queryNotifyJsonCallback(int type, String serverGBCode, String json) {
        return GS28181SDK.getInstance().queryNotifyJsonCallback(type, serverGBCode, json);
    }

    public void transDataCallback(String json,int type) {
        Log.i(TAG, "transDataCallback: " + json);
        GS28181SDK.getInstance().onTransDataNotify(json, type);
    }

    /**
     * 应用层收到订阅  -------------- OK GS28181SDK -------------
     */
    public void subscribeNotifyCallback(int type, String serverGBCode, String json) {
        Log.i(TAG, "应用层收到订阅：type= " + type + ",serverGBCode=" + serverGBCode + ",json=" + json);
        GS28181SDK.getInstance().handlerJniSubscribeNotify(type, serverGBCode, json);
    }

    public void receiveStreamCallBack(int i, byte[] bArr, long j) {
        Log.i(TAG, "receiveStreamCallBack: " + i + ",j=" + j);
        GS28181SDK.getInstance().receiveStreamCallBack(i, bArr, j);
    }

    public void receiveVideoStreamCallBack(int i, byte[] bArr, long j) {
        GS28181SDK.getInstance().receiveVideoStreamCallBack(i, bArr, j);
    }

    public void rtpStreamErrorCallBack(int i) {
        Log.i(TAG, "rtpStreamErrorCallBack: " + i);
        GS28181SDK.getInstance().rtpStreamCallBack(i);
    }

    public int getRTPStreamTranType() {
        Log.i(TAG, "getRTPStreamTranType: ");
        return GS28181SDK.getInstance().getConfig().getStreamTranType();
    }
}
